package com.maniu.mn_vip_webview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.RelativeLayout;

import com.maniu.weblib.WebConstants;
import com.maniu.weblib.view.DWebView;

import java.io.File;
import java.lang.reflect.Field;

public class MainActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.openWeb1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebActivity.start(MainActivity.this, "腾讯网",
                        "https://xw.qq.com/?f=qqcom", WebConstants.LEVEL_BASE);
            }
        });

        findViewById(R.id.openWeb2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // for account level
                WebActivity.start(MainActivity.this,
                        "AIDL测试", DWebView.CONTENT_SCHEME + "aidl.html", WebConstants.LEVEL_ACCOUNT);
            }
        });

        findViewById(R.id.zipTest).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // "file:///mnt/sdcard/test/";
                File srcZipFile = new File("/mnt/sdcard/test/html.zip");
                FileZip.unZip(srcZipFile, "/mnt/sdcard/test/");
            }
        });



        if (addPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
        }
    }

    private void testWebViewInitUsedTime() {
        long p = System.currentTimeMillis();
        WebView mWebView = new WebView(this); // webview 是一个重量级对象,第一次160 ms ~ 200 ms之间,第二次很快了8ms
        long n = System.currentTimeMillis();
        Log.e("MN--------->", "testWebViewFirstInit use time:" + (n - p));
    }

    public void webViewInit(View view) {
//        testWebViewInitUsedTime();
//        testWebViewInitUsedTime();

        RelativeLayout relativeLayout = findViewById(R.id.webViewWrap);
        BaseApplication.webView.loadUrl("https://www.baidu.com/");
        relativeLayout.removeAllViews();
        relativeLayout.addView(BaseApplication.webView);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApplication.webView.clearCache(true);//清除缓存
    }


    /**
     * 使用反射的方式来清楚WebView的缓存
     *
     * @param windowManager
     */
    public void setConfigCallback(WindowManager windowManager) {
        try {
            Field field = WebView.class.getDeclaredField("mWebViewCore");
            field = field.getType().getDeclaredField("mBrowserFrame");
            field = field.getType().getDeclaredField("sConfigCallback");
            field.setAccessible(true);
            Object configCallback = field.get(null);
            if (null == configCallback) {
                return;
            }
            field = field.getType().getDeclaredField("mWindowManager");
            field.setAccessible(true);
            field.set(configCallback, windowManager);
        } catch (Exception e) {
        }

    }

    private boolean addPermission(Context context, String permission) {
        if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
            return true;
        }
        return false;
    }
}